package com.product.service.Impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.EquityStatus;
import com.common.ResultCode;
import com.common.Util.ObjectUtil;
import com.product.config.Exception.ServiceExceptionUtil;
import com.product.controller.vo.editEquityVo;
import com.product.controller.vo.equityVo;
import com.product.dao.ProductDao;
import com.product.dao.ProductEquityDao;
import com.product.dao.ProductSupplierDao;
import com.product.entity.EquityDetailed;
import com.product.entity.Product;
import com.product.entity.ProductEquity;
import com.product.entity.ProductSupplier;
import com.product.service.IEquityDetailed;
import com.product.service.IProductEquity;
import lombok.SneakyThrows;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @program: Member_Perks_Store
 * @description:
 * @author: 刘童
 * @create: 2024-06-19 07:58
 **/

@Service
public class IProductEquityImpl extends
        ServiceImpl<ProductEquityDao, ProductEquity> implements IProductEquity {
    @Autowired
    IEquityDetailed equityDetailedDao;

    @Autowired
    ProductDao productDao;

    @Autowired
    ProductSupplierDao productSupplierDao;




    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editEquity(editEquityVo editEquityVo) {
        try {
            // 更新EquityDetailed表
            equityDetailedDao.updateById(editEquityVo.getEquityDetailed());
            // 更新Product表
            productDao.updateById(editEquityVo.getProduct());
            // 更新ProductSupplier表
            productSupplierDao.updateById(editEquityVo.getProductSupplier());
            // 更新ProductEquity表
            this.updateById(editEquityVo.getProductEquity());
        } catch (Exception e) {
            // 如果出现异常，则抛出自定义异常
            throw ServiceExceptionUtil.exception(ResultCode.INSERT_OR_UPDATE_FAILED);
        }
        return true;
    }

    @Override
    public editEquityVo getEquityByEquityId(Long equityId) {

        Product product;
        ProductSupplier productSupplier;
        EquityDetailed equityDetailed;

        // 获取产品信息
        ProductEquity productEquity = this.getById(equityId);
        if (ObjectUtils.isEmpty(productEquity)) {
            return null;// 提早返回，减少嵌套
        }

        // 使用ObjectUtils.allNotNull来避免可能的NullPointerException
        product = productDao.selectById(productEquity.getProductId());
        productSupplier = productSupplierDao.selectById(product.getProductSupplierId());
        equityDetailed = equityDetailedDao.getById(productEquity.getEquityDetailedId());
//         有任何一个对象判空，返回null
        if (!ObjectUtils.allNotNull(product, productSupplier, equityDetailed)) {
            return null;
        }
        editEquityVo editEquityVo = new editEquityVo();
        editEquityVo.setEquityDetailed(equityDetailed);
        editEquityVo.setProduct(product);
        editEquityVo.setProductSupplier(productSupplier);
        editEquityVo.setProductEquity(productEquity);

        return editEquityVo;



    }


}
